package com.bnyy.medicalHousekeeper.moudle.message.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;

import com.bnyy.medicalHousekeeper.R;
import com.bnyy.medicalHousekeeper.base.BaseActivityImpl;
import com.bnyy.medicalHousekeeper.request.BaseObserverImpl;
import com.bnyy.medicalHousekeeper.request.RequestManager;
import com.bnyy.message.bean.GroupChatSetting;
import com.kyleduo.switchbutton.SwitchButton;

import java.util.HashMap;

import butterknife.BindView;

/**
 * Author : 关炳鹏
 * Description :
 * Date : 2022/11/21
 **/
public class GroupChatManageActivity extends BaseActivityImpl {
    @BindView(R.id.switch_button_enable_edit_name)
    SwitchButton switchButtonEnableEditName;
    @BindView(R.id.switch_button_enter_confirm)
    SwitchButton switchButtonEnterConfirm;
    @BindView(R.id.switch_button_taboo)
    SwitchButton switchButtonTaboo;
    @BindView(R.id.tv_managers)
    TextView tvManagers;

    private GroupChatSetting groupChatSetting;

    public static void show(Context context, GroupChatSetting groupChatSetting, ActivityResultLauncher<Intent> launcher) {
        Intent intent = new Intent(context, GroupChatManageActivity.class);
        intent.putExtra("groupChatSetting", groupChatSetting);
        launcher.launch(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_message_group_chat_manage;
    }

    @Override
    public String getTitleStr() {
        return "群管理";
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        groupChatSetting = (GroupChatSetting) getIntent().getSerializableExtra("groupChatSetting");

        GroupChatSetting.GroupChatManage groupChatManage = groupChatSetting.getGroup_admin();

        View.OnClickListener onClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HashMap<String, Boolean> groupChatManageParams = new HashMap<>();

                switch (v.getId()) {
                    case R.id.switch_button_enable_edit_name:
                        groupChatManageParams.put("auth_rename", switchButtonEnableEditName.isChecked());
                        break;
                    case R.id.switch_button_enter_confirm:
                        groupChatManageParams.put("enter_confirm", switchButtonEnterConfirm.isChecked());
                        break;
                    case R.id.switch_button_taboo:
                        groupChatManageParams.put("prohibit_chat", switchButtonTaboo.isChecked());
                        break;
                }

                HashMap<String, Object> params = new HashMap<>();
                params.put("id", groupChatSetting.getId());
                params.put("group_admin", groupChatManageParams);

                requestManager.request(requestManager.mMessageRetrofitService.editGroupChatSetting(RequestManager.getJsonRequestBody(params)),
                        new BaseObserverImpl<Object>(mContext) {
                            @Override
                            public void onSuccess() {
                                super.onSuccess();
                                switch (v.getId()) {
                                    case R.id.switch_button_enable_edit_name:
                                        groupChatManage.setAuth_rename(switchButtonEnableEditName.isChecked());
                                        break;
                                    case R.id.switch_button_enter_confirm:
                                        groupChatManage.setEnter_confirm(switchButtonEnterConfirm.isChecked());
                                        break;
                                    case R.id.switch_button_taboo:
                                        groupChatManage.setProhibit_chat(switchButtonTaboo.isChecked());
                                        break;
                                }
                            }

                            @Override
                            public void onOtherCode(int code) {
                                super.onOtherCode(code);
                                switch (v.getId()) {
                                    case R.id.switch_button_enable_edit_name:
                                        switchButtonEnableEditName.setChecked(!switchButtonEnableEditName.isChecked());
                                        break;
                                    case R.id.switch_button_enter_confirm:
                                        switchButtonEnterConfirm.setChecked(!switchButtonEnterConfirm.isChecked());
                                        break;
                                    case R.id.switch_button_taboo:
                                        switchButtonTaboo.setChecked(!switchButtonTaboo.isChecked());
                                        break;
                                }
                            }

                            @Override
                            public void onError(Throwable e) {
                                super.onError(e);
                                switch (v.getId()) {
                                    case R.id.switch_button_enable_edit_name:
                                        switchButtonEnableEditName.setChecked(!switchButtonEnableEditName.isChecked());
                                        break;
                                    case R.id.switch_button_enter_confirm:
                                        switchButtonEnterConfirm.setChecked(!switchButtonEnterConfirm.isChecked());
                                        break;
                                    case R.id.switch_button_taboo:
                                        switchButtonTaboo.setChecked(!switchButtonTaboo.isChecked());
                                        break;
                                }
                            }
                        });
            }
        };

        switchButtonEnableEditName.setChecked(groupChatManage.isAuth_rename());
        switchButtonEnableEditName.setOnClickListener(onClickListener);

        switchButtonEnterConfirm.setChecked(groupChatManage.isEnter_confirm());
        switchButtonEnterConfirm.setOnClickListener(onClickListener);

        switchButtonTaboo.setChecked(groupChatManage.isProhibit_chat());
        switchButtonTaboo.setOnClickListener(onClickListener);

        ActivityResultLauncher<Intent> launcher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
            @Override
            public void onActivityResult(ActivityResult result) {
                if (result != null) {
                    Intent data = result.getData();
                    if (data != null) {
                        groupChatSetting = (GroupChatSetting) data.getSerializableExtra("groupChatSetting");
                    }
                }
            }
        });

        tvManagers.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                GroupChatManagersActivity.show(mContext, groupChatSetting, launcher);
            }
        });
    }

    @Override
    public void onBackPressed() {
        Intent intent = new Intent();
        intent.putExtra("groupChatSetting", groupChatSetting);
        setResult(RESULT_OK, intent);

        super.onBackPressed();
    }
}




































